// pages/category/category.js
import {request} from "../../request/index"
Page({
  data: {
    leftMenuList:[],
    rightCountent:[],
    currentIndex:0,
    scrollTop:0
  },

 Cates:[],
  onLoad: function (options) {
    const Cates = wx.getStorageSync('cates');
    if(!Cates){
      this.getCates()
    }else{
      if(Date.now()-Cates.times > 1000*10){
        this.getCates()
      }else{
        this.Cates = Cates.data
        let leftMenuList = this.Cates.map(v=>v.cat_name)
        let rightCountent = this.Cates[0].children;
        this.setData({
          leftMenuList,
          rightCountent
        })
      }
    }
  },
  async getCates(){
    const res = await request({
      url:"/categories"
    })
    this.Cates = res;
    wx.setStorageSync('cates',{
      times:Date.now(),
      data:this.Cates
    })
    let leftMenuList = this.Cates.map(v=>v.cat_name)
    let  rightCountent = this.Cates[0].children;
    this.setData({
      leftMenuList,
      rightCountent
    })
  },
  handleItemTap(e){
    const {index} = e.currentTarget.dataset;
    let rightCountent = this.Cates[index].children;
    this.setData({
      currentIndex:index,
      rightCountent,
      scrollTop:0
    })
  }
})